//获取应用实例
const app = getApp()
const request = require('../../../../utils/request');

Page({
    //初始化数据
    data: {
        testList: [0,1,2,3,4],
        addressList:[],
        noChecked: false,
        id: 0,
        idx: 0
    },
    onReady: function() {
      wx.setNavigationBarTitle({
        title: '地址管理'
      })
    },
    //显示地址
    onShow: function(){
        var that =this
      //调用接口请求数据
      request.HttpRequst(true, 'user/allAddress', false, '', { "token": wx.getStorageSync("sessionId")}, "post", false,res =>{
        console.log(res)
        if(res.code==200){
          that.setData({
            addressList: res.data,
          })
          console.log(that.data.addressList)
        }else if( res.code == 201){
          that.setData({
            addressList: [],
          })
        }else{
          return
        }
      })
    },
    //添加地址
    toAdd() {
        wx.navigateTo({
            url:'add-attr/add-attr'
        })
    },
    
    //设置默认
    setAdd(){
      
    },
    clickRadio(e) {
      this.setData({
        id: e.target.dataset.id,
        idx: e.target.dataset.index
      })
      //调用接口请求数据
      console.log(this.data.id)
      request.HttpRequst(true, 'user/defaultAddress', false, '', {"id":this.data.id, "token": wx.getStorageSync("sessionId")}, "post", false,res =>{
        console.log(res)
        if(res.code==200){
          wx.showModal({
              title: '提示',
              content: '默认地址修改成功!',
              showCancel: false,
          })
        }else{
          wx.showModal({
              title: '提示',
              content: res.info,
              showCancel: false,
          })
        }
      })
    },

    //修改地址
    revise(e) {
      let attr = JSON.stringify(e.currentTarget.dataset.attr)
      console.log(attr)
      wx.navigateTo({
        url: 'delete-attr/delete-attr?attr='+attr
      })
    }
    
})
